<template>
  <el-container>
    <el-header style="background-color:#eff2f7" class="top">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/shopList' }">数据管理</el-breadcrumb-item>
        <el-breadcrumb-item>商家列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- header右侧头像开始 -->
      <div class="block">
        <el-avatar :size="50" :src="circleUrl"></el-avatar>
        <div class="poputs">
          <div @click="tzHome">首页</div>
          <div @click="tzLogin">退出</div>
        </div>
      </div>
      <!-- header右侧头像结束 -->
    </el-header>
    <el-main style="background-color:#fff">
      <el-table :data="lists" style="width:100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="店铺名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="店铺地址">
                <span>{{ props.row.address }}</span>
              </el-form-item>
              <el-form-item label="店铺介绍">
                <span>{{ props.row.description }}</span>
              </el-form-item>
              <el-form-item label="店铺 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="联系电话">
                <span>{{ props.row.phone }}</span>
              </el-form-item>
              <el-form-item label="评分">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="销售量">
                <span>{{ props.row.recent_order_num }}</span>
              </el-form-item>
              <el-form-item label="分类">
                <span>{{ props.row.category}}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="店铺名称" prop="name"></el-table-column>
        <el-table-column label="店铺地址" prop="address"></el-table-column>
        <el-table-column label="店铺介绍" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="Success" @click="addFood(scope.$index, scope.row)">添加食品</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="20"
        :current-page.sync="currentPage1"
        :total="2327"
        background
        layout="total,prev, pager, next"
      ></el-pagination>

      <el-dialog title="修改店铺信息" :visible.sync="dialogFormVisible">
        <el-form :model="selectTable">
          <el-form-item label="店铺名称" :label-width="formLabelWidth">
            <el-input v-model="selectTable.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" :label-width="formLabelWidth">
            <el-autocomplete
              v-model="address.address"
              placeholder="请输入地址"
              style="width:100%"
            ></el-autocomplete>
            <!-- @select="addressSelect" -->
            <p>当前城市：深圳</p>
          </el-form-item>
          <el-form-item label="店铺介绍" :label-width="formLabelWidth">
            <el-input v-model="selectTable.description" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" :label-width="formLabelWidth">
            <el-input v-model="selectTable.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="店铺分类" :label-width="formLabelWidth">
            <el-cascader :options="options" v-model="selectedCategory"></el-cascader>
          </el-form-item>
          <el-form-item label="商铺图片" :label-width="formLabelWidth">
            <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt />
            </el-dialog>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      lists: [],
     circleUrl:
        "https://img01.sogoucdn.com/app/a/100520093/fb41c7c77a2454f7-01eba5833e7e38bc-11cc0200d4b834a0dddb28091fd46806.jpg",
      currentPage1: 1,
      offset: 0,
      limit: 20,
      options: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      selectTable: {},
      deleteTable:{},
      address: {},
      dialogImageUrl: "",
      dialogVisible: false,
      selectedCategory:'',
    };
  },
  mounted() {
    //商品列表的数据
    this.$axios
      .get(
        "https://elm.cangdu.org/shopping/restaurants?latitude=22.54286&longitude=114.059563&offset=0&limit=20"
      )
      .then((res) => {
        // console.log(res);
        this.lists = res.data;
      });
    //dialog里面的食品分类的数据
    this.$axios
      .get("https://elm.cangdu.org/shopping/v2/restaurant/category")
      .then((res) => {
        res.data.forEach((item) => {
          if (item.sub_categories.length) {
            const addnew = {
              value: item.name,
              label: item.name,
              children: [],
            };
            item.sub_categories.forEach((subitem, index) => {
              if (index == 0) {
                return;
              }
              addnew.children.push({
                value: subitem.name,
                label: subitem.name,
              });
            });
            this.options.push(addnew);
          }
        });
      });
  },
  methods: {
     //头像
    tzHome(){
      this.$router.push({path:'/home'});
    },
    tzLogin(){
      this.$router.push({path:'/login'})
    },
    //同步信息
    handleEdit(index, row) {
      // console.log(row);
      this.selectTable = row;
      this.address.address = row.address;
      this.dialogFormVisible = true;
      this.selectedCategory = row.category.split("/");
    },
    //添加食物
    addFood(index, row) {
      this.$router.push({ path: "addGood" });
    },
    //删除食物
     handleDelete(index, row) {
      try {
        let deleteTable = row;
        // console.log(deleteTable);
        if (row.status == 1) {
          this.$message({
            type: "success",
            message: "删除店铺成功",
          });
          this.row.splice(index, 1);
        } else {
          throw new Error(res.message);
        }
      } catch (err) {
        this.$message({
          type: "error",
          message: err.message,
        });
        console.log("删除店铺失败");
      }
    },
    // async querySearchAsync(queryString, cb) {
    //   if (queryString) {
    //     try {
    //       const cityList = await searchplace(this.city.id, queryString);
    //       if (cityList instanceof Array) {
    //         cityList.map((item) => {
    //           item.value = item.address;
    //           return item;
    //         });
    //         cb(cityList);
    //       }
    //     } catch (err) {
    //       console.log(err);
    //     }
    //   }
    // },
    // addressSelect(vale) {
    //   const { address, latitude, longitude } = vale;
    //   this.address = { address, latitude, longitude };
    // },
    //编辑里面的图片上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //分页器的数据交互
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // 重新发请求
      // this.lists = [];
      this.$axios
        .get(
          `https://elm.cangdu.org/shopping/restaurants?latitude=22.54286&longitude=114.059563&offset=${
            (val - 1) * this.limit
          }&limit=20`
        )
        .then((res) => {
          this.lists = res.data;
          console.log(res.data);
        });
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>


<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-table {
  border: 1px solid #ccc;
}
.el-button--mini {
  padding: 4px;
  font-size: 12px;
  border-radius: 4px;
}
/* 头像 */
.block {
  position: relative;
}
.poputs {
  position: absolute;
  top: 55px;
  left: -20px;
  width: 80px;
  height: 60px;
  background-color: #fff;
  box-sizing: border-box;
  box-shadow: 2px 3px 10px -1px #333;
  display: none;
  z-index: 999;
}
.block:hover .poputs {
  display: block;
}
.poputs > div {
  width: 80px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  color: #000;
  cursor: pointer;
}
.poputs > div:hover {
  background-color: #e4e8f1;
  color: #333;
}
</style>